package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.edu.bean.*;
import com.zy.edu.config.SystemParam;
import com.zy.edu.config.ali.AliVideo;
import com.zy.edu.dao.CourseChapterVideoDao;
import com.zy.edu.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-24
 */
@Service
@Transactional
public class CourseChapterVideoServiceImpl extends ServiceImpl<CourseChapterVideoDao, CourseChapterVideo> implements CourseChapterVideoService {


    @Resource
    UserService userService;
    @Resource
    CourseChapterService courseChapterService;
    @Resource
    CourseChapterVideoClsService courseChaptVideoClsService;
    @Resource
    CourseChapterVideoStuService courseChapterVideoStuService;
    @Override
    public Map saveOrUpdate(CourseChapterVideo courseChapterVideo, InputParam inputParam) {

        if(StringUtils.isEmpty(courseChapterVideo.getChapterId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }

        //当前用户的信息
        User currentUser = userService.selectById(inputParam.getUserId());
        //只有超管和校管可以上传课时
        if(currentUser.getRoleType().equals(Role.SUPER)
                ||currentUser.getRoleType().equals(Role.SCHOOL)) {
            if (StringUtils.isEmpty(courseChapterVideo.getId())) {

                CourseChapter courseChapter = courseChapterService.selectById(courseChapterVideo.getChapterId());
                if(courseChapter == null){
                    return ResponseInfo.error(SystemParam.ERROR_PARAMS);
                }
                courseChapterVideo.setCourseId(courseChapter.getCourseId());

                courseChapterVideo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                courseChapterVideo.setCreateTime(new Date());
                courseChapterVideo.setStatus(1);
                this.baseMapper.insert(courseChapterVideo);
            } else {
                this.baseMapper.updateById(courseChapterVideo);
            }
            return ResponseInfo.success(null);
        }else {
            return ResponseInfo.error("您无权限创建课时");
        }
    }

    @Override
    public Map list(CourseChapterVideo courseChapterVideo, InputParam inputParam) {
        if(StringUtils.isEmpty(courseChapterVideo.getChapterId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
        EntityWrapper<CourseChapterVideo> entityWrapper = new EntityWrapper<CourseChapterVideo>();
        if(!StringUtils.isEmpty(courseChapterVideo.getName())){
            entityWrapper.like("name",courseChapterVideo.getName());
        }
        entityWrapper.eq("chapter_id",courseChapterVideo.getChapterId());
        entityWrapper.eq("status",1);
        entityWrapper.orderBy("num",true);
        List<CourseChapterVideo> chapterList = this.baseMapper.selectList(entityWrapper);
        return ResponseInfo.success(chapterList);
    }

    @Override
    public Map dele(CourseChapterVideo chapter) throws Exception {
        if(!StringUtils.isEmpty(chapter.getId())){
            //设置为不可用
            chapter.setStatus(0);
//            this.baseMapper.updateById(chapter);
            //关联删除阿里云的视频
            CourseChapterVideo courseChapterVideo = this.baseMapper.selectById(chapter.getId());
            AliVideo.deleteVideo(courseChapterVideo.getUrl());
            //视频直接删除
            this.baseMapper.deleteById(chapter);
        }
        return ResponseInfo.success(null);
    }

    @Override
    public Map getById(CourseChapterVideo courseChapterVideo, InputParam inputParam) {
        if(StringUtils.isEmpty(courseChapterVideo.getId())) {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }else{
            CourseChapterVideo temp = this.selectById(courseChapterVideo.getId());

            User user = userService.selectById(inputParam.getUserId());
            //获取班级该视频的播放记录
            if(user.getRoleType().equals(Role.TEACHER)){
                EntityWrapper<CourseChapterVideoCls> tempQ = new EntityWrapper<>();
                tempQ.eq("video_id",courseChapterVideo.getId())
                        .eq("class_id",inputParam.getClassId());
                List<CourseChapterVideoCls> list = courseChaptVideoClsService.selectList(tempQ);
                if (list.size()>0) {
                    CourseChapterVideoStu stu = new CourseChapterVideoStu();
                    BeanUtils.copyProperties(list.get(0),stu);
                    temp.setRecord(stu);
                }
            }else if(user.getRoleType().equals(Role.STUDENT)){
                //学生该视频的播放记录
                EntityWrapper<CourseChapterVideoStu> tempQ = new EntityWrapper<>();
                tempQ.eq("video_id",courseChapterVideo.getId())
                        .eq("user_id",inputParam.getUserId());
                List<CourseChapterVideoStu> list = courseChapterVideoStuService.selectList(tempQ);
                if (list.size()>0) {
                    temp.setRecord(list.get(0));
                }
            }
            return ResponseInfo.success(temp);
        }

    }

    @Override
    public Map saveRecord(CourseChapterVideoCls courseChapterVideoCls, InputParam inputParam) {
        //当前用户的信息
        User temp = userService.selectById(inputParam.getUserId());
        //教师播放视频
        if(temp.getRoleType().equals(Role.TEACHER)){
            courseChaptVideoClsService.saveOrUpdate(courseChapterVideoCls,inputParam);
        }else if(temp.getRoleType().equals(Role.STUDENT)){
            //学生观看视频
            CourseChapterVideoStu stu = new CourseChapterVideoStu();
            BeanUtils.copyProperties(courseChapterVideoCls,stu);
            stu.setUserId(inputParam.getUserId());
            courseChapterVideoStuService.saveOrUpdate(stu,inputParam);
        }


        return ResponseInfo.success(null);
    }
}
